<?php
if($chave == "rtg4df65g4t"){
    $cont = 0;
    
    // acessa o banco
    while ($banco[$cont]){
        $b = explode(";", $banco[$cont]);
        $database = $b[2];

        $saida = '';
        $saida .= "/* ---------------------------------------- */\n";
        $saida .= "/* Banco: $database */\n";
        $saida .= "/* ---------------------------------------- */\n";
        $saida .= "\n";

        $sql = "show tables";
        $db = new db($banco[$cont]);
        $conexao = $db->conecta();
        $result = $db->query($sql, $conexao);

        // percorre as tabelas
        while ($tables = $db->fetch_array($result)){
            $sql_tab = "SELECT * FROM $tables[0]";
            $res_tab = $db->query($sql_tab, $conexao);

            $saida .= "/* ---------- Tabela: $tables[0] ---------- */\n\n";
            
            // gera SQLs de inserçao
            while ($dados = $db->fetch_array($res_tab)){
                $saida .= "INSERT INTO $database.$tables[0] VALUES(";
                $val = '';
                foreach ($dados as $index => $value) {
                    if(!valida::numero($index)){
                        if($val != ''){ $val .= ", "; }
                        $val .= "\"$value\"";
                    }
                }
                $saida .= $val.");\n";
            }
            $saida .= "\n/* ---------------------------------------- */\n";
        }
        $saida .= "\n\n\n";

        $db->close($conexao);
        unset($db);
        $cont++;
        
        // gera arquivo SQL
        $file = fopen( "tmp/$temp_dir/".$temp_dir."_$database.sql", "w" );
        fwrite($file, $saida);
        fclose($file);
    }
}